<template>
  <div>
    <titleLabe title="4 长期稳定性试验放样信息"/>
    <div class="box_table" v-for="(val,key,idx) in dataTable" :key="idx">
      <div class="title">{{val.name}}的稳定性试验放样信息</div>
      <table border="1" style="width: 100%" cellspacing="0" cellpadding="10">
        <thead>
        <tr>
          <th>名称</th>
          <th>批号</th>
          <th>单倍量</th>
          <th>2倍量</th>
          <th>3倍量</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="(item,index) in val.list" :key="index">
          <td align="center">{{item.sample.sample_name}}</td>
          <td align="center">{{ handlSample(item.sample.batch_info)}}</td>
          <td align="center">{{item.value }}{{item.sample.packaging_unit}}</td>
          <td align="center"> {{item.value}} {{item.sample.packaging_unit}} * 2 = {{item.value *
            2}}{{item.sample.packaging_unit}}
          </td>
          <td align="center"> {{item.value}} {{item.sample.packaging_unit}} * 3 = {{item.value *
            3}}{{item.sample.packaging_unit}}
          </td>
        </tr>
        </tbody>
      </table>
    </div>

    <titleLabe title="5 样品统计量"/>
    <div class="box_table">
      <table border="1" style="width: 100%" cellspacing="0" cellpadding="10">
        <thead>
        <tr>
          <template v-for="(cel,index) in headData">
            <th v-if="cel.label.label === '放置条件'" rowspan="2">{{cel.label.label}}</th>
            <th colspan="3" v-else>{{cel.label.label}}</th>
          </template>
        </tr>
        <tr>
          <template v-for="(cel,index) in headData">
            <template v-if="cel.label.label !== '放置条件'">
              <th>单倍量</th>
              <th>使用倍数</th>
              <th>小计</th>
            </template>
          </template>
        </tr>
        </thead>
        <tbody>
        <tr v-for="(item,index1) in experiment" :key="index1">
          <template v-for="(cel,index2) in headData">
            <td align="center" :key="index2" v-if="cel.label.label === '放置条件'">{{item[cel.prop]}}</td>
            <template v-for="(col,ind2) in cel.children">
              <td align="center" v-if="col.label === '使用倍数'">{{item[col.prop]}}</td>
              <td align="center" v-else-if="col.label === '小计'">
                {{calculate(col.prop,cel.label.id,index1)}}
                {{calculate(col.prop,cel.label.id,index1) === '/' ? '':col.unit}}
              </td>
              <td align="center" v-else>
                {{item[col.prop] === 0 ? '/':item[col.prop]}}
                {{item[col.prop] === 0 ? '':col.unit}}
              </td>
            </template>
          </template>

        </tr>
        <tr>
          <td align="center">总量</td>
          <td align="center" v-for="(item,index) in headData.filter(p=>p.children.length>0)" :key="index"
              :colspan="item.children.length">
            {{ combined(item.label.id,item.children[0].unit)}}
          </td>
        </tr>
        </tbody>
      </table>
    </div>

  </div>
</template>

<script>
  import titleLabe from './title'

  export default {
    name: 'lofting',
    components: {
      titleLabe
    },
    props: {
      experiment: {
        type: Array,
        default: function() {
          return []
        }
      },
      checkData: {
        type: Object,
        default: function() {
          return {}
        }
      }
    },
    data() {
      return {
        data: {},
        dataTable: {},
        tableData: [],
        headData: [
          {
            label: { label: '放置条件' },
            prop: 'type',
            children: []
          }
        ]
      }
    },
    //是计算属性，也可以理解为一个方法。
    computed: {
      combined() {
        return function(val, unit) {
          let sun = 0, a, b
          for (let i = 0; i < this.experiment.length; i++) {
            a = `小计-${val}`, b = this.experiment[i]
            sun += b[a]
            sun = Math.ceil(sun)
            if (isNaN(sun)) {
              sun = 0
            }
          }
          return `${sun}${unit}` || 0
        }
      },
      handlSample() {
        return function(val) {
          let str = ''
          for (let i = 0; i < val.length; i++) {
            str += val[i].sample_batch
            if (i !== val.length - 1) {
              str = str + '、'
            }
          }
          return str
        }
      }
    },

    //属性是手动定义的所需监听的值
    watch: {
      checkData: {
        handler: function(newV, oldV) {
          this.data = newV
          this.headData = [
            {
              label: { label: '放置条件' },
              prop: 'type',
              children: []
            }
          ]
          this.handlData()
        }
      }
    },

    //生命周期 - 创建完成（访问当前this实例）
    created() {
    },

    //生命周期 - 挂载完成（访问DOM元素）
    mounted() {
    },

    //是Vue实例对象上绑定的方法，供当前Vue组件作用域内使用，
    methods: {
      //处理数据
      handlData() {
        let arr = [],
          list = []
        for (let key in this.data) {
          for (let i = 0; i < this.data[key].total.a.length; i++) {
            arr.push(this.data[key].total.a[i].arrList)
          }
        }
        if (arr.length === 0) return
        list = arr.reduce((a, b) => {
          return a.concat(b)
        })
        let data = {}, data1 = {}
        for (let j = 0; j < list.length; j++) {
          if (!data1[list[j].idts]) {
            data1[list[j].idts] = { name: list[j].name, list: [] }
          }
          let index = data1[list[j].idts].list.findIndex(a => a.sample.id == list[j].sample.id)
          if (index === -1) {
            data1[list[j].idts].list.push(list[j])
          }

          let name = `${list[j].typeName}${list[j].name}`
          let obj = {
            name: name,
            idts: list[j].idts,
            list: []
          }
          this.$set(data, name, obj)
        }

        for (let key in data) {
          data[key].list = data1[data[key].idts].list
        }

        this.dataTable = data
        let b = []
        for (let key in data) {
          let a = {
            type: data[key].name
          }
          for (let s = 0; s < data[key].list.length; s++) {
            a[data[key].list[s].sample.id] = data[key].list[s].value
          }
          b.push(a)
        }
        this.handlHead(list, b)
      },

      //生成表头
      handlHead(list, val) {
        let data = {}
        for (let i = 0; i < list.length; i++) {
          data[list[i].sample.id] = list[i].sample
        }
        let aaa = []
        for (let key in data) {
          let a = {
            label: {
              id: data[key].id,
              label: data[key].sample_name
            },
            prop: '',
            children: [
              {
                label: '单倍量',
                prop: data[key].id,
                unit: data[key].packaging_unit
              },
              {
                label: '使用倍数',
                prop: '使用倍数' + '-' + data[key].id,
                unit: data[key].packaging_unit
              },
              {
                label: '小计',
                prop: '小计' + '-' + data[key].id,
                unit: data[key].packaging_unit
              }
            ]
          }
          aaa.push(a)
        }

        this.headData = this.headData.concat(aaa)
        this.handlTbale(val)
      },

      //生成表格数据
      handlTbale(val) {
        let arr = [],
          data = {}
        for (let i = 0; i < this.headData.length; i++) {
          data[this.headData[i].prop] = ''
          data.boole = true
          if (this.headData[i].children.length > 0) {
            for (let j = 0; j < this.headData[i].children.length; j++) {
              data[this.headData[i].children[j].prop] = 0
            }
          }
        }
        for (let d = 0; d < val.length; d++) {
          arr.push(data)
          arr[d].type = val[d].name
          let obj1 = arr[d],
            obj2 = val[d]
          Object.keys(obj1).forEach(key => {
            obj1[key] = obj2[key] || obj1[key]
          })
        }

        if (this.tableData.length > 0) {
          this.tableData = this.experiment || []
        } else {
          this.tableData = JSON.parse(JSON.stringify(arr))
        }
      },

      calculate(prop, label, index) {
        let a = '使用倍数' + '-' + label,
          b = label
        let sss = (this.experiment[index][a] * this.experiment[index][b])
        if (this.experiment[index][b] === 0) {
          sss = '/'
        } else {
          sss = Math.ceil(sss)
          if (isNaN(sss)) {
            return 0
          }
        }
        return sss
      }
    }

  }

</script>


<style scoped>
  .title {
    text-align: center;
    line-height: 50px;
  }
</style>
